package com.tengxing.e1.FactoryTest;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Stress_Test_Log_Activity extends AppCompatActivity {
    private static final String TAG = "Stress_Test_Log_Activity";
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    TextView tV_Stress_Log;
    Button btn_Stress_Pass, btn_Stress_Fail,
            btn_Stress_Log_All_Read, btn_Stress_Log_All_Del,
            btn_Stress_Log_DLL_Read, btn_Stress_Log_DLL_Del,
            btn_Stress_Log_IMU_Read, btn_Stress_Log_IMU_Del,
            btn_Stress_Log_SUS_Read, btn_Stress_Log_SUS_Del,
            btn_Stress_Log_TMP_Read, btn_Stress_Log_TMP_Del,
            btn_Stress_Log_Err_Read, btn_Stress_Log_Err_Del;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_stress_test_log);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        sharedPreferences = getSharedPreferences("Test_Result", MODE_PRIVATE);
        tV_Stress_Log = findViewById(R.id.tV_Stress_Log);
        //Log_Read("stress_test_log_all.txt");
        Log_Read("stress_test_log_err.txt");
        Factory_Stress_Log_BTN();
    }

    private void Factory_Stress_Log_BTN() {
        btn_Stress_Pass = findViewById(R.id.btn_Stress_Pass);
        btn_Stress_Pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor = sharedPreferences.edit();
                editor = editor.putString("Stress", "通过");
                if (editor.commit()) {
                    Toast.makeText(Stress_Test_Log_Activity.this, "测试结果保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Stress_Test_Log_Activity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(Stress_Test_Log_Activity.this, "测试结果保存失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_Stress_Fail = findViewById(R.id.btn_Stress_Fail);
        btn_Stress_Fail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor = sharedPreferences.edit();
                editor = editor.putString("Stress", "未通过");
                if (editor.commit()) {
                    Toast.makeText(Stress_Test_Log_Activity.this, "测试结果保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Stress_Test_Log_Activity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(Stress_Test_Log_Activity.this, "测试结果保存失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_Stress_Log_All_Read = findViewById(R.id.btn_Stress_Log_All_Read);
        btn_Stress_Log_All_Read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Read("stress_test_log_all.txt");
            }
        });

        btn_Stress_Log_All_Del = findViewById(R.id.btn_Stress_Log_All_Del);
        btn_Stress_Log_All_Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Del("stress_test_log_all.txt");
            }
        });

        btn_Stress_Log_DLL_Read = findViewById(R.id.btn_Stress_Log_DLL_Read);
        btn_Stress_Log_DLL_Read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Read("stress_test_log_DLL.txt");
            }
        });

        btn_Stress_Log_DLL_Del = findViewById(R.id.btn_Stress_Log_DLL_Del);
        btn_Stress_Log_DLL_Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Del("stress_test_log_DLL.txt");
            }
        });

        btn_Stress_Log_IMU_Read = findViewById(R.id.btn_Stress_Log_IMU_Read);
        btn_Stress_Log_IMU_Read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Read("stress_test_log_IMU.txt");
            }
        });

        btn_Stress_Log_IMU_Del = findViewById(R.id.btn_Stress_Log_IMU_Del);
        btn_Stress_Log_IMU_Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Del("stress_test_log_IMU.txt");
            }
        });

        btn_Stress_Log_SUS_Read = findViewById(R.id.btn_Stress_Log_SUS_Read);
        btn_Stress_Log_SUS_Read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Read("stress_test_log_SUS.txt");
            }
        });

        btn_Stress_Log_SUS_Del = findViewById(R.id.btn_Stress_Log_SUS_Del);
        btn_Stress_Log_SUS_Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Del("stress_test_log_SUS.txt");
            }
        });

        btn_Stress_Log_TMP_Read = findViewById(R.id.btn_Stress_Log_TMP_Read);
        btn_Stress_Log_TMP_Read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Read("stress_test_log_TMP.txt");
            }
        });

        btn_Stress_Log_TMP_Del = findViewById(R.id.btn_Stress_Log_TMP_Del);
        btn_Stress_Log_TMP_Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Del("stress_test_log_TMP.txt");
            }
        });

        btn_Stress_Log_Err_Read = findViewById(R.id.btn_Stress_Log_Err_Read);
        btn_Stress_Log_Err_Read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Read("stress_test_log_err.txt");
            }
        });

        btn_Stress_Log_Err_Del = findViewById(R.id.btn_Stress_Log_Err_Del);
        btn_Stress_Log_Err_Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log_Del("stress_test_log_err.txt");
            }
        });
    }
    private void Log_Del(String log_file_name){
        try {
            OutputStream os = openFileOutput(log_file_name, Context.MODE_PRIVATE);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write("");
            bw.close();
            os.close();
        } catch (IOException e) {
            Log.e(TAG, "new_txt_err:"+e);
            throw new RuntimeException(e);
        }
    }

    private void Log_Read(String log_file_name){
        try {
            OutputStream os = openFileOutput(log_file_name, Context.MODE_APPEND);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.close();
            os.close();
        } catch (IOException e) {
            Log.e(TAG, "new_txt_err:"+e);
            throw new RuntimeException(e);
        }

        String stress_log;
        try {
            InputStream is = openFileInput(log_file_name);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuilder stress_log_bd = new StringBuilder();
            while((line = br.readLine()) != null){
                stress_log_bd.append(line);
                stress_log_bd.append('\n');
            }
            br.close();
            is.close();
            stress_log = stress_log_bd.toString();

        } catch (IOException e) {
            Log.e(TAG, "read_txt_err:"+e);
            throw new RuntimeException(e);
        }
        tV_Stress_Log.setText(stress_log);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(Stress_Test_Log_Activity.this, MainActivity.class);
        startActivity(intent);
        finish();
        Toast.makeText(Stress_Test_Log_Activity.this, "直接退出不会保存结果", Toast.LENGTH_SHORT).show();
        super.onBackPressed();
    }
}